package org.example.myleet.p1022;

import org.example.myleet.Utils.TreeNode;

public class Solution {

    private int sum;

    public int sumRootToLeaf(TreeNode root) {
        sum = 0;
        dfs(root, 0);
        return sum;
    }

    private void dfs(TreeNode node, int num) {
        int n = (num << 1) + node.val;
        if (null == node.left && null == node.right) {
            sum += n;
            return;
        }
        if (null != node.left) {
            dfs(node.left, n);
        }
        if (null != node.right) {
            dfs(node.right, n);
        }
    }
}
